﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.Maiyoule.GsmSms.Components {
    /// <summary>
    /// 协议数据单元类型 应答路径
    /// </summary>
    enum PduTypeRP {
        /// <summary>
        /// 未设置
        /// </summary>
        UNSETTING =0,
        /// <summary>
        /// 设置
        /// </summary>
        SETTING = 1
    }
    /// <summary>
    /// 用户数据头标识
    /// </summary>
    enum PduTypeUDHI {
        /// <summary>
        /// 不包含头信息
        /// </summary>
        EMPTY = 0,
        /// <summary>
        /// 包含头信息
        /// </summary>
        INCLUDE = 1
    }
    /// <summary>
    /// 请求状态报告
    /// </summary>
    enum PduTypeSRR {
        /// <summary>
        /// 不需要
        /// </summary>
        NO = 0,
        /// <summary>
        /// 需要
        /// </summary>
        YES = 1
    }
    /// <summary>
    /// 状态报告指示
    /// </summary>
    enum PduTypeSRI {
        /// <summary>
        /// 不会返回消息实体
        /// </summary>
        NO = 0,
        /// <summary>
        /// 返回消息实体
        /// </summary>
        YES = 1
    }

    /// <summary>
    /// 有效期格式
    /// </summary>
    enum PduTypeVPF {
        /// <summary>
        /// 没有提供
        /// </summary>
        NONE = 0,
        /// <summary>
        /// 保留
        /// </summary>
        KEEP = 1,
        /// <summary>
        /// 整型形式
        /// </summary>
        INTEGER = 2,//10
        /// <summary>
        /// 位组一半形式 如 13/01/01 00:00:01
        /// </summary>
        GROUP = 3//11
    }
    /// <summary>
    /// 拒绝复本（Reject Duplicate）
    /// </summary>
    enum PduTypeRD {
        /// <summary>
        /// 通知短消息服务中心（SMSC）接受一个消息（SMS-SUBMIT），即该消息是先前已提交过的，并还存在与SMSC中未发送出去。MS重复的条件是：消息参考（MR）、接收方地址（DA)及发送方地址（OA）相同
        /// </summary>
        SUBMIT = 0,
        /// <summary>
        /// 通知SMSC拒绝一个重复的SMS
        /// </summary>
        CONTINUE = 1
    }
    /// <summary>
    /// 有更多的消息需要发送（More Message to Send），此值仅被SMSC设置
    /// </summary>
    enum PduTypeMMS {
        /// <summary>
        /// 在SMSC中有更多的信息等待MS
        /// </summary>
        MORE = 0,
        /// <summary>
        /// 在SMSC中没有更多的信息等待MS
        /// </summary>
        NONE =1
    }
    /// <summary>
    /// 信息类型指示
    /// </summary>
    enum PduTypeMTI {
        /// <summary>
        /// 发送
        /// </summary>
        SEND = 1,
        /// <summary>
        /// 接收
        /// </summary>
        RECEIVE = 0
    }
    /// <summary>
    /// 协议数据单元类型（PDU-Type）
    /// </summary>
    class ProtocolDataUnit {

        public ProtocolDataUnit() {
            this.rp = PduTypeRP.UNSETTING;
            this.udhi = PduTypeUDHI.EMPTY;
            this.sri = PduTypeSRI.NO;
            this.srr = PduTypeSRR.NO;
            this.mms = PduTypeMMS.NONE;
            this.mti = PduTypeMTI.SEND;
            this.vpf = PduTypeVPF.INTEGER;
            this.rd = PduTypeRD.CONTINUE;

        }
        private PduTypeRP rp;
        /// <summary>
        /// 应答路径
        /// </summary>
        public PduTypeRP Rp {
            get { return rp; }
            set { rp = value; }
        }
        private PduTypeUDHI udhi;
        /// <summary>
        /// 用户数据头标识
        /// </summary>
        public PduTypeUDHI Udhi {
            get { return udhi; }
            set { udhi = value; }
        }
        private PduTypeSRR srr;
        /// <summary>
        /// 请求状态报告
        /// </summary>
        public PduTypeSRR Srr {
            get { return srr; }
            set { srr = value; }
        }
        private PduTypeVPF vpf;
        /// <summary>
        /// 有效期格式
        /// </summary>
        public PduTypeVPF Vpf {
            get { return vpf; }
            set { vpf = value; }
        }
        private PduTypeRD rd;
        /// <summary>
        /// 拒绝复本
        /// </summary>
        public PduTypeRD Rd {
            get { return rd; }
            set { rd = value; }
        }
        private PduTypeMTI mti;
        /// <summary>
        /// 信息指示类型
        /// </summary>
        public PduTypeMTI Mti {
            get { return mti; }
            set { mti = value; }
        }
        private PduTypeSRI sri;
        /// <summary>
        /// 状态报告指示
        /// </summary>
        public PduTypeSRI Sri {
            get { return sri; }
            set { sri = value; }
        }
        private PduTypeMMS mms;
        /// <summary>
        /// 有更多的消息需要发送
        /// </summary>
        public PduTypeMMS Mms {
            get { return mms; }
            set { mms = value; }
        }


        /// <summary>
        /// 获取发送的PDU-Type
        /// </summary>
        /// <returns></returns>
        public string GetSendPDUType() {
            string result = "";
            result += this.rp==PduTypeRP.UNSETTING?"0":"1";
            result += this.udhi==PduTypeUDHI.EMPTY?"0":"1";
            result += this.srr==PduTypeSRR.NO?"0":"1";
            result += Util.Oct2BinString((int)this.vpf);// this.vpf.ToString();
            result += this.rd==PduTypeRD.SUBMIT?"0":"1";
            result += this.mti==PduTypeMTI.RECEIVE?"00":"01";

            return Util.BinString2HexString(result);
        }

        /// <summary>
        /// 获取接收的PDU类型
        /// </summary>
        /// <returns></returns>
        public string GetReceivePDUType() {
            string result = "";
            result += this.rp == PduTypeRP.UNSETTING ? "0" : "1";
            result += this.udhi == PduTypeUDHI.EMPTY ? "0" : "1";
            result += this.srr == PduTypeSRR.NO ? "0" : "1";
            result += "00";
            result += this.mms==PduTypeMMS.MORE?"0":"1";
            result += this.mti == PduTypeMTI.RECEIVE ? "00" : "01";

            return Util.BinString2HexString(result);
        }


        public void SetPDUType(string pdutypestr) {
            //转换为二进制字符串
            string binstr = Util.HexString2BinString(pdutypestr);
            if (binstr.Length != 8) {
                return;
            }

            string mitstr = binstr.Substring(6, 2);
            if (mitstr == PduTypeMTI.SEND.ToString()) {
                //发送的
                this.mti = PduTypeMTI.SEND;
                this.rp = (PduTypeRP)Enum.Parse(typeof(PduTypeRP), binstr.Substring(0, 1));
                this.udhi = (PduTypeUDHI)Enum.Parse(typeof(PduTypeUDHI), binstr.Substring(1, 1));
                this.srr = (PduTypeSRR)Enum.Parse(typeof(PduTypeSRR), binstr.Substring(2, 1));
                this.vpf = (PduTypeVPF)Enum.Parse(typeof(PduTypeVPF), binstr.Substring(3, 2));
                this.rd = (PduTypeRD)Enum.Parse(typeof(PduTypeVPF), binstr.Substring(5, 1));
                this.mti = (PduTypeMTI)Enum.Parse(typeof(PduTypeVPF), binstr.Substring(6, 2));
            } else {
                //接收的
                this.mti = PduTypeMTI.RECEIVE;
                this.rp = (PduTypeRP)Enum.Parse(typeof(PduTypeRP), binstr.Substring(0, 1));
                this.udhi = (PduTypeUDHI)Enum.Parse(typeof(PduTypeUDHI), binstr.Substring(1, 1));
                this.srr = (PduTypeSRR)Enum.Parse(typeof(PduTypeSRR), binstr.Substring(2, 1));
                this.vpf = (PduTypeVPF)Enum.Parse(typeof(PduTypeVPF), binstr.Substring(3, 2));
                this.mms = (PduTypeMMS)Enum.Parse(typeof(PduTypeMMS), binstr.Substring(5, 1));
                this.mti = (PduTypeMTI)Enum.Parse(typeof(PduTypeVPF), binstr.Substring(6, 2));
            }


        }
    }
}
